package mi.payment.ui.admin;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CompoundButton;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.settings.SalesSlipPrintSettings;
import mi.payment.settings.SalesSlipPrintSettingsOperation;
import mi.payment.ui.base.ParentActivity;
import sunmi.sunmiui.button.ButtonSwitch;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.EditTextDialog;
import sunmi.sunmiui.dialog.ListDialog;
import sunmi.sunmiui.title.Title;

/**
 * 签购单参数
 * Created by lj on 2018/1/23.
 */

public class SalesSlipParamsActivity extends ParentActivity {

    private View selectSalesSlipRiseView;  //签购单抬头选择
    private View serviceHotLineSetView;    //服务热线设置
    private View salesSlipFontView;        //签购单字号设置
    private View vIsPrinterEnglish;        //签购单是否打印英文
    private View vIsPrintSubtitle;        //签购单是否打印副标题

    private TextView rightText1;
    private TextView rightText2;
    private TextView rightText4;


    private ButtonSwitch bsIsPrinterEnglish, bsIsPrintSubtitle;   //签购单是否打印英文

    private EditTextDialog mEditTextDialog;

    private ListDialog mListDialog;
    private SalesSlipPrintSettingsOperation mSalesSlipPrintSettingsOperation;
    private SalesSlipPrintSettings mSalesSlipPrintSettings;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sales_slip_params);
        mSalesSlipPrintSettingsOperation = SalesSlipPrintSettingsOperation.getInstance();
        initTitle();
        initView();
        setClickListener();
        setDataBySelect();
    }

    /**
     * 初始化Title
     */
    private void initTitle() {
        Title mTitle;
        mTitle = (Title) findViewById(R.id.sign_singles_print_params_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.sign_singles_params));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SalesSlipParamsActivity.this.finish();
            }
        }, null, null, null);
    }

    /**
     * 初始化界面
     */
    private void initView() {
        TextView leftText;
        selectSalesSlipRiseView = findViewById(R.id.select_sales_slip_rise);
        serviceHotLineSetView = findViewById(R.id.service_hotline_set);
        salesSlipFontView = findViewById(R.id.sales_slip_font);
        vIsPrinterEnglish = findViewById(R.id.invoice_printing_print_english);
        vIsPrintSubtitle = findViewById(R.id.invoice_printing_print_subtitle);

        leftText = (TextView) selectSalesSlipRiseView.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.select_sales_slip_rise1));

        leftText = (TextView) serviceHotLineSetView.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.service_hotline_set));

        leftText = (TextView) salesSlipFontView.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.sales_slip_font));


        leftText = (TextView) vIsPrinterEnglish.findViewById(R.id.switch_item_text);
        leftText.setText(getResources().getString(R.string.invoice_printing_print_english));

        leftText = (TextView) vIsPrintSubtitle.findViewById(R.id.switch_item_text);
        leftText.setText(getResources().getString(R.string.invoice_printing_print_subtitle));

        rightText1 = (TextView) selectSalesSlipRiseView.findViewById(R.id.item_right_text);
        rightText2 = (TextView) serviceHotLineSetView.findViewById(R.id.item_right_text);
        rightText4 = (TextView) salesSlipFontView.findViewById(R.id.item_right_text);
        rightText4.setText(getResources().getString(R.string.sales_slip_font_small));

        bsIsPrinterEnglish = (ButtonSwitch) vIsPrinterEnglish.findViewById(R.id.set_switch);
        bsIsPrintSubtitle = (ButtonSwitch) vIsPrintSubtitle.findViewById(R.id.set_switch);
        setOnCheckedListener();
    }

    /**
     * 设置switch按钮的监听事件
     */
    private void setOnCheckedListener() {
        bsIsPrinterEnglish.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mSalesSlipPrintSettingsOperation.saveSalesSlipPrintEnglish(isChecked);
                setDataBySelect();
            }
        });

        bsIsPrintSubtitle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mSalesSlipPrintSettingsOperation.savePrintSubtitle(isChecked);
                setDataBySelect();
            }
        });
    }

    /**
     * 设置item的监听事件
     */
    private void setClickListener() {
        selectSalesSlipRiseView.setOnClickListener(this);
        serviceHotLineSetView.setOnClickListener(this);
        salesSlipFontView.setOnClickListener(this);
        vIsPrinterEnglish.setOnClickListener(this);
        vIsPrintSubtitle.setOnClickListener(this);
    }

    /**
     * 填充数据
     */
    private void setDataBySelect() {
        mSalesSlipPrintSettings = mSalesSlipPrintSettingsOperation.getSalesSlipPrintSettings();
        if (mSalesSlipPrintSettings.getRise() == 0) {
            if (TextUtils.isEmpty(mSalesSlipPrintSettings.getRiseString())) {
                rightText1.setText(getResources().getString(R.string.manual_input));
            } else {
                rightText1.setText(mSalesSlipPrintSettings.getRiseString());
            }
        } else if (mSalesSlipPrintSettings.getRise() == 1) {
            rightText1.setText(getResources().getString(R.string.default_sales_slips));
        } else if (mSalesSlipPrintSettings.getRise() == 2) {
            rightText1.setText(getResources().getString(R.string.sales_slip_picture));
        } else {
            rightText1.setText(getResources().getString(R.string.select_sales_slip_rise2));
        }
        if (TextUtils.isEmpty(mSalesSlipPrintSettings.getHotLine())) {
            rightText2.setText(getResources().getString(R.string.service_hotline_set2));
        } else {
            rightText2.setText(mSalesSlipPrintSettings.getHotLine());
        }
        if (mSalesSlipPrintSettings.getSelectSalesSlipFont() == 0) {
            rightText4.setText(getResources().getString(R.string.sales_slip_font_small));
        } else {
            rightText4.setText(getResources().getString(R.string.sales_slip_font_big));
        }
        bsIsPrinterEnglish.setOnCheck(mSalesSlipPrintSettings.isSalesSlipPrintEnglish());
        bsIsPrintSubtitle.setOnCheck(mSalesSlipPrintSettings.isPrintSubtitle());
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.select_sales_slip_rise:
                //签购单抬头选择
                String[] item = {getResources().getString(R.string.manual_input), getResources().getString(R.string.default_sales_slips), getResources().getString(R.string.sales_slip_picture)};
                mListDialog = DialogCreater.createListDialog(this, getResources().getString(R.string.set_to), getResources().getString(R.string.cancel), item);
                mListDialog.setItemClickListener(mItemClickListener);
                mListDialog.show();
                break;
            case R.id.service_hotline_set:
                //服务热线设置
                mEditTextDialog = DialogCreater.createEditTextDialog(SalesSlipParamsActivity.this, getString(R.string.cancel),
                        getString(R.string.save), getString(R.string.service_hotline_set), new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mEditTextDialog.dismiss();
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mSalesSlipPrintSettingsOperation.saveHotLine(mEditTextDialog.getText());
                                setDataBySelect();
                                mEditTextDialog.dismiss();
                            }
                        }, new DialogInterface.OnShowListener() {
                            @Override
                            public void onShow(DialogInterface dialog) {
                                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_PHONE);
                                showSoftKeyboard(mEditTextDialog);
                            }
                        });
                if (TextUtils.isEmpty(mSalesSlipPrintSettings.getHotLine())) {
                    mEditTextDialog.setHintText(getResources().getString(R.string.return_unknow));
                } else {
                    mEditTextDialog.setText(mSalesSlipPrintSettings.getHotLine());
                }
                mEditTextDialog.show();
                break;
            case R.id.sales_slip_font: {
                //签购单字号设置
                String[] text = {getResources().getString(R.string.sales_slip_font_select_small), getResources().getString(R.string.sales_slip_font__select_big)};
                mListDialog = DialogCreater.createListDialog(this, getResources().getString(R.string.sales_slip_font), getResources().getString(R.string.cancel), text);
                mListDialog.setItemClickListener(salesSlipFontItemListener);
                mListDialog.show();
                break;
            }
            case R.id.invoice_printing_print_english:
                //签购单是否打印英文
                bsIsPrinterEnglish.setOnCheck(!bsIsPrinterEnglish.isChecked());
                break;
            case R.id.invoice_printing_print_subtitle:
                //签购单是否打印副标题
                bsIsPrintSubtitle.setOnCheck(!bsIsPrintSubtitle.isChecked());
                break;
            default:
                break;
        }
    }


    private ListDialog.ItemClickListener mItemClickListener = new ListDialog.ItemClickListener() {
        @Override
        public void OnItemClick(int position) {
            switch (position) {
                case 0:
                    mListDialog.dismiss();
                    mEditTextDialog = DialogCreater.createEditTextDialog(SalesSlipParamsActivity.this, getString(R.string.cancel),
                            getString(R.string.save), getString(R.string.purchase_order_header1), new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    mEditTextDialog.dismiss();
                                }
                            }, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if (checkEmpty(mEditTextDialog.getText())) {
                                        mSalesSlipPrintSettingsOperation.saveRise(0);
                                        mSalesSlipPrintSettingsOperation.saveRiseString(mEditTextDialog.getText());
                                        setDataBySelect();
                                        mEditTextDialog.dismiss();
                                    }
                                }
                            }, new DialogInterface.OnShowListener() {
                                @Override
                                public void onShow(DialogInterface dialog) {
                                    showSoftKeyboard(mEditTextDialog);
                                }
                            });
                    if (TextUtils.isEmpty(mSalesSlipPrintSettings.getRiseString())) {
                        mEditTextDialog.setHintText(getResources().getString(R.string.return_unknow));
                    } else {
                        mEditTextDialog.setText(mSalesSlipPrintSettings.getRiseString());
                    }
                    mEditTextDialog.show();

                    break;
                case 1:
                    mSalesSlipPrintSettingsOperation.saveRise(1);
                    mSalesSlipPrintSettingsOperation.saveRiseString(" ");
                    setDataBySelect();
                    mListDialog.dismiss();
                    break;
                case 2:
                    mSalesSlipPrintSettingsOperation.saveRise(2);
                    mSalesSlipPrintSettingsOperation.saveRiseString(" ");
                    setDataBySelect();
                    mListDialog.dismiss();
                    break;
            }
        }
    };


    private ListDialog.ItemClickListener salesSlipFontItemListener = new ListDialog.ItemClickListener() {
        @Override
        public void OnItemClick(int position) {
            switch (position) {
                case 0:
                    mListDialog.dismiss();
                    //签购单子号设置为 小
                    mSalesSlipPrintSettingsOperation.saveSelectSalesSlipFont(0);
                    setDataBySelect();
                    break;
                case 1:
                    mListDialog.dismiss();
                    //签购单子号设置为 大
                    mSalesSlipPrintSettingsOperation.saveSelectSalesSlipFont(1);
                    setDataBySelect();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeDialog();
    }

    private void closeDialog() {
        try {
            if (mListDialog != null) {
                mListDialog.dismiss();
                mListDialog = null;
            }
            if (mEditTextDialog != null) {
                mEditTextDialog.dismiss();
                mEditTextDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
